package fragments.shopfragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.travel.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import shopbean.ShopSearchBean;
import shopbean.contans.SearchBean;
import shopbean.contans.ShopURL;
import tools.Okhttputils;

/**
 * Created by Administrator on 2016/10/24.
 */
public class ToFragment extends Fragment {
    @Bind(R.id.shop_fragment_list)
    ListView shopFragmentList;
    private View view;
    private  String serachContent;
    private ToAdapter adapter;
    private List<SearchBean.DataBean.ResBean> listto=new ArrayList<>();
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            listto= (List<SearchBean.DataBean.ResBean>) msg.obj;
            adapter=new ToAdapter(getContext(),listto);
            shopFragmentList.setAdapter(adapter);
        }
    };
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.shop_fragment_list, null);
        ButterKnife.bind(this, view);
        Bundle bundle=this.getArguments();
        if(bundle!=null)
        serachContent=bundle.getString("bu");
//        Log.i("tag","----serachContent---"+serachContent);
        inData();
        return view;
    }
    private void inData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString= Okhttputils.getInstance().run(ShopURL.SEARCHTOH+serachContent+ShopURL.SEARCHTOF);
                    JSONObject obj=JSONObject.parseObject(jsonString).getJSONObject("data");
                    String res=obj.getString("res");
                    List<SearchBean.DataBean.ResBean> list=new ArrayList<SearchBean.DataBean.ResBean>();
                    list= JSONArray.parseArray(res,SearchBean.DataBean.ResBean.class);
                    Message message=new Message();
                    message.obj=list;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
